package com.wanhe.base.util;

import java.text.DecimalFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by taosj on 2015/7/21.
 */
public class StringFormatUtil {

	/**
	 * 保留小数点后两位
	 * @param number 数值
	 * @return
	 */
	public static String afterDecimalTwo(double number){
		DecimalFormat decimalFormat=new DecimalFormat(".00");//构造方法的字符格式这里如果小数不足2位,会以0补足.
		return decimalFormat.format(number);//format 返回的是字符串
	}

	/**
	 * 替换掉空格
	 * @param content
	 * @return
	 */
	public static String replaceBlank(String content){
		Pattern p = Pattern.compile("//s*|/t|/r|/n");
		Matcher m = p.matcher(content);
		content = m.replaceAll("");
		return content;
	}

	/**
	 * 去掉辅料名字后的（辅料）字样
	 * @return
	 */
	public static String cuiYuGarnishName(String garnishName){
		garnishName = replaceBlank(garnishName);
		if(garnishName.contains("（") ){
			garnishName = garnishName.split("（")[0];
		}

		if(garnishName.contains("(")){
			garnishName = garnishName.split("\\(")[0];
		}
		return garnishName;
	}

	public static String cuiYuName(String garnishName){
		garnishName = replaceBlank(garnishName);
		if(garnishName.contains("）") ){
			garnishName = garnishName.split("）")[1];
		}

		if(garnishName.contains(")")){
			garnishName = garnishName.split("\\)")[1];
		}
		return garnishName;
	}

	/**
	 *删除口味最后两位字符
	 * @param name
	 * @return
	 */
	public static String delLastTwo(String name){
		name = replaceBlank(name);
		int len = name.length();
		if(len > 2){
			name = name.substring(0, len-2);
		}
		return name;
	}

}
